﻿var map = null;
var myGer = null;
var mouseTool = null;
var MapLngPoint = 120.08;
var MapLatPoint = 27.78;
var MapName = "文成县";
function EnterpriseMapSearch(Unid) {
    mouseTool = new AMap.MouseTool(map);
    mouseTool.marker();
    mouseTool.on('draw', function (e) {
        if (e.obj.CLASS_NAME = "AMap.Marker") {
            var enterpriseUnid = $("#enterpriseUnid").val();
            var position = e.obj.G.position;
            //更新数据库定位数据
            var path = position.lng + "," + position.lat;
            jQuery.usi.core.post("/Enterprise/UpdatePoint?Unid=" + enterpriseUnid + "&path=" + path, "", false,function (resultData) {
                var point = ConvertPoint(resultData.data.enterpriseForMap.baiduPoint);
                AddMarket(0, point);
                return;
            });
        }
    });
    jQuery.usi.core.post("/Enterprise/GetEnterpriseById", { unid: Unid },false, function (data) {
        var newpoint;
        if (jQuery.usi.core.isNullOrEmpty(data.data.baiduPoint)) {
            myGer.getLocation(data.data.address, function (status, result) {
                if (status === 'complete' && result.info === 'OK') {
                    geocoder_CallBack(result);
                }
            });
        } else {
            newpoint = ConvertPoint(data.data.baiduPoint);
            AddMarket(0, newpoint)
        }
        $("#enterpriseUnid").val(Unid);
    });
}
//地理编码返回结果展示
function geocoder_CallBack(data) {
    var resultStr = "";
    //地理编码结果数组
    var geocode = data.geocodes;
    for (var i = 0; i < geocode.length; i++) {
        //更新数据库定位数据
        var path = geocode[i].location.getLng() + "," + geocode[i].location.getLat();
        jQuery.usi.core.post("/Enterprise/UpdatePoint?Unid=" + $("#enterpriseUnid").val() + "&path=" + path, "",false, function (resultData) {
            AddMarket(i, ConvertPoint(path));
        });
    }
}
function AddMarket(i, d) {
    map.clearMap();
    var marker = new AMap.Marker({
        map: map,
        position: [d.getLng(), d.getLat()]
    });
}
function ConvertPoint(paths) {
    return new AMap.LngLat(paths.split(',')[0], paths.split(',')[1]);
}
    